我必须在zip文件中创建两个文件夹,我用编程方式创建ICSharpCode.SharZipLib.Zip.我想要:
private void AddToZipStream(byte[] inputStream, ZipOutputStream zipStream, string fileName, string fileExtension)
{
var courseName = RemoveSpecialCharacters(fileName);
var m_Bytes = inputStream;
if ((m_Bytes != null) && (zipStream != null))
{
var newEntry = new ZipEntry(ZipEntry.CleanName(string.Concat(courseName, fileExtension)));
newEntry.DateTime = DateTime.Now;
newEntry.Size = m_Bytes.Length;
zipStream.PutNextEntry(newEntry);
zipStream.Write(m_Bytes, 0, m_Bytes.Length);
zipStream.CloseEntry();
zipStream.UseZip64 = UseZip64.Off;
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用ZipEntry以及如何将文件添加到Zip存档内的目录中来创建目录?
我想压缩一个InputStream使用ZipOutputStream,然后InputStream从压缩ZipOutputStream而不保存光盘上的文件.那可能吗?
我正在使用 aZipOutputStream来压缩一堆文件,这些文件是已压缩格式以及许多大型高度可压缩格式(如纯文本)的混合。
大多数已压缩的格式都是大文件,花费 CPU 和内存来重新压缩它们是没有意义的,因为它们永远不会变小,有时在极少数情况下会变得稍大。
当我检测到预压缩文件时我尝试使用.setMethod(ZipEntry.STORED),但它抱怨我需要为这些文件提供size, compressedSize and crc。
我可以使用以下方法让它工作,但这需要我读取文件两次。一旦计算CRC32然后再次将文件实际复制到ZipOutputStream.
// code that determines the value of method omitted for brevity
if (STORED == method)
{
fze.setMethod(STORED);
fze.setCompressedSize(fe.attributes.size());
final HashingInputStream his = new HashingInputStream(Hashing.crc32(), fis);
ByteStreams.copy(his,ByteStreams.nullOutputStream());
fze.setCrc(his.hash().padToLong());
}
else
{
fze.setMethod(DEFLATED);
}
zos.putNextEntry(fze);
ByteStreams.copy(new FileInputStream(fe.path.toFile()), zos);
zos.closeEntry();
Run Code Online (Sandbox Code Playgroud)
ZipOutputStream仅压缩文件夹中的文件.我也想压缩子文件夹.我怎样才能做到这一点?
如何将文件附加到现有的 zip 文件?我已经有了可以创建 zip 文件的代码,除了一个大问题外,它运行良好。现在的工作方式是,用户拍摄一堆照片,最后,所有照片都被添加到一个 zip 文件中,如果你拍了足够多的照片,这可能需要相当长的时间。:-( 所以我在想,我有一个非常好的和有效的解决方案。在拍摄照片时,我会在拍摄后立即将每张新照片添加到 zip 文件中。然后当他们完成拍照时,完成 zip 文件,使其可用并导出。:-)
问题是,我无法将文件添加到现有的 zip 文件中。:-( 这是我到目前为止所拥有的。另外,请记住,这只是一个概念证明,我确实理解为 for 循环的每次迭代重新初始化所有内容是非常愚蠢的。循环的每次迭代都是应该代表正在添加的另一个文件,这很可能会在很长时间之后,甚至可能是一个小时之后,这就是为什么我每次迭代都重置所有内容,因为该应用程序将在添加文件之间关闭。如果我能让这个工作,然后我实际上会放弃 for 循环并将此代码放入每次拍摄照片时都会调用的函数中。:-)
try {
for(int i=0; i < _files.size(); i++) {
//beginning of initial setup stuff
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(_zipFile,false);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
byte data[] = new byte[BUFFER];
out.setLevel(0); //I added this because it makes it not compress the data
//at all and I hoped that it would allow the zip to be appended …Run Code Online (Sandbox Code Playgroud) 我使用 ZipOutputStream 创建了 Zip 文件。zip 文件已成功创建,并且能够使用 WinRar 和 Winzip 文件打开。这些文件显示在 Zip 文件内。
但同一个文件,我无法在 Windows 7 默认 Zip Extractor 中打开。尝试使用 Windows 7 默认提取器进行提取时,Zip 文件内没有文件。但实际上 Zip 文件内有多个可用文件。
请帮忙解决这个问题...谢谢..
我有一个jsp/servlet web应用程序,客户端可以通过下拉框选择"课程"和"作业",然后单击按钮下载该课程/作业组合下列出的数据库中的所有文件.servlet代码不能正常工作,因为zip文件没有作为附件发送到浏览器.我确实有一个工作代码一次下载一个文件,但是有些东西被这个代码卡在了压缩文件中.数据库中的所有文件实际上都是zip文件本身,所以我试图压缩一堆zip文件.我不认为这会要求他们的处理方式与压缩任何其他格式的文件不同.有谁能看到缺少的东西?这是我处理下载的servlet中的doGet方法代码.这些代码大部分都是在stackoverflow上找到的.请注意,FileSubmitted对象是我的DOA,包含数据库中每个文件的所有文件信息,包括Blob本身:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
List<FileSubmitted> fileList = new ArrayList<FileSubmitted>();
String course= request.getParameter("course");
String assignment = request.getParameter("assignment");
java.sql.PreparedStatement pstmt = null;
java.sql.Connection conn = null;
ResultSet rs;
String queryString;
try {
conn = ConnectionManager.getConnection();
conn.setAutoCommit(false);
queryString = "SELECT * FROM files WHERE courseID=\""+course+"\" AND assignmentID=\""+assignment+"\";";
pstmt = conn.prepareStatement(queryString);
rs = pstmt.executeQuery(queryString);
while(rs.next())
{
fileList.add(new FileSubmitted(rs.getString("username"),
rs.getString("courseID"),
rs.getString("assignmentID"),
rs.getString("fileName"),
rs.getString("mimeType"),
(Blob) rs.getBlob("contents")));
}
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=\"allfiles.zip\"");
ZipOutputStream output = null;
byte[] buffer = …Run Code Online (Sandbox Code Playgroud) 我尝试将 Zip 从 a 复制Zipinputstream到 a Zipoutputstream。
我将 Zip 存储byte[]在 Oracle 数据库中。我Zipinputstream用来解压缩 zip(后来我想编辑 Zip),然后将它放入 aZipoutputstream以获取新的byte[]并使用此数组稍后通过.zip下载文件ServletOutputStream。当我创建一个新文件时 - 没有Zipinputstream- 它可以工作。但是当我使用时,Zipinputstream我得到了错误。
这是我的代码:
ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(fileFromDataBase),
Charset.forName("UTF-8"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(byteArrayOutputStream, Charset.forName("UTF-8"));
ZipEntry currentEntry;
byte[] buffer = new byte[8192];
while ((currentEntry = zipInputStream.getNextEntry()) != null) {
ZipEntry newEntry = new ZipEntry(currentEntry.getName());
zos.putNextEntry(newEntry);
int length;
while ((length = zipInputStream.read(buffer)) …Run Code Online (Sandbox Code Playgroud) 我的MapReduce必须从HBase读取记录并需要写入zip文件.我们的客户特别询问减速器输出文件应该只是.zip文件.
为此,我编写了ZipFileOutputFormat包装来压缩记录并写入zip文件.
此外,我们不能使用缓冲区并将所有行保留到缓冲区然后迭代,因为某些文件包含19GB的记录,那时它将抛出一个java.lang.OutOfMemoryError.
一切似乎都好,但有一个问题:
该.zip是越来越为每个键创建的文件.在我的输出文件中,我可以看到许多输出文件,这些是每行键分隔文件.我不知道如何将它组合在zip文件中.
这是我的实施 ZipFileOutputFormat.java
public class ZipFileOutputFormat<K, V> extends FileOutputFormat<K, V> {
public static class ZipRecordWriter<K, V> extends org.apache.hadoop.mapreduce.RecordWriter<K, V> {
private ZipOutputStream zipOut;
public ZipRecordWriter(FSDataOutputStream fileOut) {
zipOut = new ZipOutputStream(fileOut);
}
@Override
public void close(TaskAttemptContext context) throws IOException, InterruptedException {
// TODO Auto-generated method stub
zipOut.closeEntry();
zipOut.finish();
zipOut.close();
zipOut.flush();
}
@Override
public void write(K key, V value) throws IOException {
String fname = null;
if (key instanceof …Run Code Online (Sandbox Code Playgroud) 我正在尝试压缩在字符串上转换的 Xml 列表,将它们仅保存在一个 zip 文件中,并在 Restful 上作为 POST 的正文返回。但是每次我保存文件时,我都会收到错误消息“存档格式未知或已损坏”。
protected ByteArrayOutputStream zip(Map<String, String> mapConvertedXml) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
try {
for(Map.Entry<String, String> current : mapConvertedXml.entrySet()){
ZipEntry entry = new ZipEntry(current.getKey() + ".xml");
entry.setSize(current.getValue().length());
zos.putNextEntry(entry);
zos.write(current.getValue().getBytes());
zos.flush();
}
zos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return baos;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我吗?