- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 Java 程序,它搜索包含昨天日期的文件夹并将其压缩为 7zip 文件并在最后删除它。现在我注意到我的程序生成的 7zip 存档文件太大了。当我使用像 7-Zip 文件管理器这样的程序来压缩我的文件时,它会生成一个 5 kb 大的存档,而我的程序会为相同的文件(具有 873 kb 大小)生成一个 737 kb 大的存档。现在恐怕我的程序不会将其压缩为 7zip 文件,而是压缩为普通的 zip 文件。有没有办法更改我的代码中的某些内容,以便它生成一个较小的 7zip 文件,就像 7-Zip 文件管理器那样?
package SevenZip;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
public class SevenZipUtils {
public static void main(String[] args) throws InterruptedException, IOException {
String sourceFolder = "C:/Users/Ferid/Documents/Dates/";
String outputZipFile = "/Users/Ferid/Documents/Dates";
int sleepTime = 0;
compress(sleepTime, outputZipFile, sourceFolder);
}
public static boolean deleteDirectory(File directory, int sleepTime) throws InterruptedException {
if (directory.exists()) {
File[] files = directory.listFiles();
if (null != files) {
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
deleteDirectory(files[i], sleepTime);
System.out.println("Folder deleted: " + files[i]);
} else {
files[i].delete();
System.out.println("File deleted: " + files[i]);
}
}
}
}
TimeUnit.SECONDS.sleep(sleepTime);
return (directory.delete());
}
public static void compress(int sleepTime, String outputZipFile, String sourceFolder)
throws IOException, InterruptedException {
// finds folder of yesterdays date
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1); // date of yesterday
String timeStamp = new SimpleDateFormat("yyyyMMdd").format(cal.getTime()); // format the date
System.out.println("Yesterday was " + timeStamp);
if (sourceFolder.endsWith("/")) { // add yesterday folder to sourcefolder path
sourceFolder = sourceFolder + timeStamp;
} else {
sourceFolder = sourceFolder + "/" + timeStamp;
}
if (outputZipFile.endsWith("/")) { // add yesterday folder name to outputZipFile path
outputZipFile = outputZipFile + " " + timeStamp + ".7z";
} else {
outputZipFile = outputZipFile + "/" + timeStamp + ".7z";
}
File file = new File(sourceFolder);
if (file.exists()) {
try (SevenZOutputFile out = new SevenZOutputFile(new File(outputZipFile))) {
addToArchiveCompression(out, file, ".");
System.out.println("Files sucessfully compressed");
deleteDirectory(new File(sourceFolder), sleepTime);
}
} else {
System.out.println("Folder does not exist");
}
}
private static void addToArchiveCompression(SevenZOutputFile out, File file, String dir) throws IOException {
String name = dir + File.separator + file.getName();
if (file.isFile()) {
SevenZArchiveEntry entry = out.createArchiveEntry(file, name);
out.putArchiveEntry(entry);
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int count = 0;
while ((count = in.read(b)) > 0) {
out.write(b, 0, count);
}
out.closeArchiveEntry();
in.close();
System.out.println("File added: " + file.getName());
} else if (file.isDirectory()) {
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
addToArchiveCompression(out, child, name);
}
}
System.out.println("Directory added: " + file.getName());
} else {
System.out.println(file.getName() + " is not supported");
}
}
}
我正在使用 Apache Commons Compress library
编辑:这是一个 link我从中获得了一些 Apache Commons Compress 代码。
最佳答案
Commons Compress 正在为每个存档条目在容器文件中启动一个新 block 。注意这里的 block 计数器:
不是您所希望的答案,但文档说它不支持“固体压缩”——将多个文件写入一个 block 。请参阅文档中的第 5 段 here .
快速浏览了一下,发现了其他一些支持 LZMA 压缩的 Java 库,但我无法在 7-Zip 的父容器文件格式中找到可以这样做的库。也许其他人知道替代方案...
这听起来像是普通的 zip 文件格式(例如通过 ZipOutputStream )不是一个选项?
关于Java 7zip 压缩太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54075881/
您好,我在我的项目中使用 Nuxt JS,我注意到我的 js 文件变得相当大 我的问题是如何缩小或拆分超过 1mb 的 vendor 或 js 文件 我还看到 font-awesome 也占用了很多空
github 只是告诉我,而不是提交差异——关于我的一个提交—— Sorry, this diff is taking too long to generate. It may be too larg
这个非常简单的绘图命令创建了一个 11x11 像素的跟踪红色矩形: _sp.graphics.lineStyle( 1, 0xFF0000, 1, true, LineScaleMode.NORMAL
我正在尝试创建一个 Mac 应用程序。但是在我使用 Interface Builder 插入组合框并运行应用程序后,我收到以下消息: This application is trying to dra
我通过 url 传递以下数据: '; echo 'Download PDF'; echo ''; ?> 这里$result包含数组形式的 RSS 提要数据。我正在使用 urlencode(
我正在尝试通过 JavaScript 从某人的网络摄像头拍摄快照。该代码可以工作,只是生成的图像对我来说被拉伸(stretch)太多而难以阅读。 我搞乱了:ctx.drawImage(video, 0
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 3年前关闭。 Improve this
我是 React 和 Webpack 代码分割的新手,所以我不确定这对于捆绑 block 来说是否合适。 我尝试根据路由 ( basically done the same thing as in t
我正在开发自己的电子书应用程序。我的书共有 15 章,因此我有一个包含 15 个元素的列表 - 每个元素都是一个包含所有章节文本的字符串。 我还有 15 个 .ogg 文件,是我花钱请配音人员录制的。
我正在部署一个应用程序,该应用程序具有大约 10 个库依赖项(我正在使用 cocoapods)和 appstore 上 30MB 的下载大小,但是当我归档产品以供 Appstore 上传时,xcarc
我正在开发一款全屏游戏,我正在尽最大努力确保它适合所有屏幕尺寸。这被证明是相当困难的,因为当我使用 pygame.FULLSCREEN 时,窗口比屏幕大得多。为了解决这个问题,我根据 python 将
我有一个项目,其中有一些UIbuttons,其中显示了不同的UIimages。通过用户交互,UIButtons 中可能存在任何 UIimages。该项目中有大约 1000 张图像。我已经初始化了一个名
我使用 Eclipse 中的 Window Builder 将 JTextField 添加到 JPanel 中,无论我尝试什么,文本字段都会显示如下: (来源:gyazo.com) 我尝试更改首选尺寸
我有一个查询,我正在选择一个大型数据集,并且由于连接表的一对多关系而花费了非常多的时间。我有三个表——简化后如下: 客户 | customers | CREATE TABLE `customers`
我在 Activity 中使用了一个 PopupWindow,除了 PopupWindow 中包含的元素的填充外,一切正常> - 它太大了 - 几乎占据了 PopupWindows 的大部分小空间。这
这个问题在这里已经有了答案: NSString intValue not working for retrieving phone number (2 个答案) 关闭 9 年前。
我对为什么我的图片大小不正确感到有点困惑。我将 44x44 大小的图像传递给默认行高为 44 像素的 UITableView,但我得到以下结果: 这不是我应该如何设置我的 UITableViewCel
我目前正在用 php 显示用户个人资料图片,如果他们单击它,就会转到他们自己的个人资料。图像显示正确并且链接有效,但链接的区域超出了图片并超过了附近的链接。我将如何减少 anchor 的面积?
这张图片太大了,我想让它适合窗口的大小:http://zgaming.comxa.com 但是我不明白怎么办,我也试过高度和宽度100% html CSS .bgimg { posi
减少矩阵大小的方法在哪里? (x2 阵列)例如,我只需要将数据 (0, 1, 2) 存储到数组中但元素最多可达 250 000。?有没有办法存储值,比如在字典中......? const int MA
我是一名优秀的程序员,十分优秀!